namespace Plugins.Controls
{
    using System;
    using System.Security;
    using System.Windows.Forms;
    using Plugins.Library;

    public static class PluginControlHelper
    {
        public static void Action<T>(T control, Action<T> method) where T : Control
        {
            try
            {
                if (control != null && method != null)
                {
                    PluginInvoke.Action<T>(control, method);
                }
            }
            catch (SecurityException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
        }

        public static void ContainerAction<T>(T control, Action<T> method) where T : ContainerControl
        {
            try
            {
                if (control != null && method != null)
                {
                    PluginInvoke.ContainerAction<T>(control, method);
                }
            }
            catch (SecurityException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
        }

        public static void TreeViewAction<T>(T control, Action<TreeView> method) where T : TreeView
        {
            try
            {
                if (control != null && method != null)
                {
                    if (control.IsHandleCreated && !(control.Disposing || control.IsDisposed))
                    {
                        control.Invoke(method, control);
                    }
                }
            }
            catch (SecurityException ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
        }

    }
}

